using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MoveIt.Framework;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ParticleEngine2D;

namespace MoveIt.Object.Player
{
    public class Ball : CAnimationObject
    {
        private float m_Factor;
        ParticleEngine particleEngine;
        public float Factor
        {
            get { return m_Factor; }
            set { m_Factor = value; }
        }

        Vector2 m_PositionOld;

        public Ball(Vector2 _Position)
            : base(_Position)
        {
            m_Factor = 0.7f;
            m_IDObject = IDObject.BALL;
            m_Sprite = new CSprite(CResourceManager.GetInstance().GetResource(IDResource.BALL));
            Sprite.Origin = new Vector2(Sprite.FrameWidht / 2, Sprite.FrameHeight / 2);
            m_PositionOld = m_Position;
            List<Texture2D> textures = new List<Texture2D>();
            textures.Add(CResourceManager.GetInstance().GetTexture(IDTexture.BLADE));
            particleEngine = new ParticleEngine(textures, this.Position);
        }

        public override void UpdateMovement(GameTime _GameTime, CInput _Input)
        {
            m_PositionOld = m_Position;
            base.Update(_GameTime, _Input);
            base.UpdateMovement(_GameTime, _Input);
            Velocity = new Vector2(_Input.AccelReading.X * m_Factor, _Input.AccelReading.Y * m_Factor);
            particleEngine.Update();
            particleEngine.EmitterLocation = this.Position;
            CheckOutScreen();
        }

        public override void UpdateCollision(CAnimationObject _Object)
        {
            
            if (CheckCollision(_Object) != DirectCollision.NONE)
            {
                switch (_Object.IDObject)
                {
                    case IDObject.HARDLINE:
                        if (CheckCollision(_Object) == DirectCollision.BOTTOM)
                        {
                            this.m_Position.Y = _Object.Position.Y + _Object.Sprite.FrameHeight / 2 + this.Sprite.FrameHeight / 2 + 1;
                            this.Velocity = new Vector2(this.Velocity.X, 0.0f);
                        }
                        else if (CheckCollision(_Object) == DirectCollision.TOP)
                        {
                            this.m_Position.Y = _Object.Position.Y - _Object.Sprite.FrameHeight / 2 - this.Sprite.FrameHeight / 2 - 1;
                            this.Velocity = new Vector2(this.Velocity.X, 0.0f);
                        }
                        else if (CheckCollision(_Object) == DirectCollision.RIGHT)
                        {
                            this.m_Position.X = _Object.Position.X + _Object.Sprite.FrameWidht / 2 + this.Sprite.FrameWidht / 2 + 1;
                            this.Velocity = new Vector2(0.0f, this.Velocity.Y);
                        }
                        else if (CheckCollision(_Object) == DirectCollision.LEFT)
                        {
                            this.m_Position.X = _Object.Position.X - _Object.Sprite.FrameWidht / 2 - this.Sprite.FrameWidht / 2 - 1;
                            this.Velocity = new Vector2(0.0f, this.Velocity.Y);
                        }
                        break;
                    default:
                        break;
                }
            }
            base.UpdateCollision(_Object);
        }

        private void CheckOutScreen()
        {
            if (this.m_Position.X <= this.GetBound().Width / 2)
            {
                m_Position.X = this.GetBound().Width / 2;
            }
            else if (this.m_Position.X + this.GetBound().Width/2 >= GlobalSetting.SCREEN_WIDTH)
            {
                m_Position.X = GlobalSetting.SCREEN_WIDTH - Sprite.FrameWidht/2;
            }
            if (this.Position.Y <= this.GetBound().Height / 2)
            {
                m_Position.Y = this.GetBound().Height / 2;
            }
            else if (this.Position.Y + this.GetBound().Height/2 >= GlobalSetting.SCREEN_HEIGHT)
            {
                m_Position.Y = GlobalSetting.SCREEN_HEIGHT - Sprite.FrameHeight/2;
            }
        }

        public override void Draw(SpriteBatch _SpriteBatch)
        {
            base.Draw(_SpriteBatch);
            //particleEngine.Draw(_SpriteBatch); 
        }
    }
}
